package com.heima.wemedia.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmNewsMaterial;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Slf4j
@Transactional
public class WmNewsServiceImpl  extends ServiceImpl<WmNewsMapper, WmNews> implements WmNewsService {

    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;

    @Override
    public ResponseResult list(WmNewsPageReqDto dto) {

        //1，分页条件参数判断
        dto.checkParam();

        //2，条件餐顺判断
        LambdaQueryWrapper<WmNews> queryWrapper=new LambdaQueryWrapper<>();
        //2.1判断当前状态
        if(dto.getStatus()!=null){
            queryWrapper.eq(WmNews::getStatus,dto.getStatus());
        }
        //2.2判断所属频道
        if(dto.getChannelId()!=null){
            queryWrapper.eq(WmNews::getChannelId,dto.getChannelId());
        }
        //2.3判断关键字
        if (dto.getKeyword()!=null){
            queryWrapper.like(WmNews::getTitle, dto.getKeyword());
        }
        //2.4判断发布时间
        if(dto.getBeginPubdate()!=null && dto.getEndPubdate()!=null){
            queryWrapper.between(WmNews::getPublishTime,dto.getBeginPubdate(),dto.getEndPubdate());
        }
        //3，开始执行分页查询


        IPage<WmNews> iPage=new Page<>(dto.getPage(),dto.getSize());

        iPage = page(iPage, queryWrapper);
        //4，封装数据

        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), Integer.parseInt(iPage.getTotal()+""));
        result.setData(iPage.getRecords());
        return result;
    }





    /**
     * 发布文章，保存草稿，提交审核和修改
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult submit(WmNewsDto dto) {
        //1.参数判断
        //判断content文章内容不能为空
        if(dto.getContent()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.保存或者修改文章
        WmNews wmNews=new WmNews();
        BeanUtils.copyProperties(dto,wmNews);
        //封装数据用户id
        wmNews.setUserId(WmThreadLocalUtil.getUser().getId());
        //创建时间
        wmNews.setCreatedTime(new Date());
        //封面图片
        if(dto.getImages().size()>0 && dto.getImages()!=null){
            String images = StringUtils.join(dto.getImages(), ",");
            wmNews.setImages(images);
        }
        saveOrUpdateWmNews(wmNews);

        //如果是草稿
        if(dto.getStatus()==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.CAO_GAO);
        }
        //3.文章与内容图片的关联

        //4.文章与封面图片的关联


        return null;
    }

    /**
     * 抽取的第一个方法，保存或者修改wm_news表
     * @param wmNews
     */
    private void saveOrUpdateWmNews(WmNews wmNews) {
        //新增
        if(wmNews.getId()==null){
            //新增文章
            save(wmNews);
        }else{
            //修改
            //先删除文章素材中间表
            wmNewsMaterialMapper.delete(Wrappers.<WmNewsMaterial>lambdaQuery().eq(WmNewsMaterial::getNewsId,wmNews.getId()));
            //修改文章
            updateById(wmNews);
        }
    }

}
